package com.itheima.stock.service.impl;

import com.google.common.base.Strings;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.UserService;
import com.itheima.stock.utils.IdWorker;
import com.itheima.stock.vo.rep.LoginReqVo;
import com.itheima.stock.vo.resp.LoginRespVo;
import com.itheima.stock.vo.resp.R;
import com.itheima.stock.vo.resp.ResponseCode;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/*
 * 用户服务实现
 * */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public R<LoginRespVo> login(LoginReqVo vo) {
        //1.判断vo,用户名,密码,效验码,sessionid是否存在
        if (vo == null || Strings.isNullOrEmpty(vo.getUsername()) || Strings.isNullOrEmpty(vo.getPassword())
                || Strings.isNullOrEmpty(vo.getCode()) || Strings.isNullOrEmpty(vo.getRkey())) {
            //快速淘汰效验码,合理利用redis的内存空间
            redisTemplate.delete(vo.getRkey());
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        //效验验证码
        //获取redis下key对应的效验码
        String redisCode = (String) redisTemplate.opsForValue().get(vo.getRkey());
        //比对
        if (redisCode == null || !redisCode.equals(vo.getCode())) {
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        //快速淘汰效验码,合理利用redis的内存空间
        redisTemplate.delete(vo.getRkey());

        //2.根据用户名查询用户是否存在
        SysUser userInfo = sysUserMapper.findUserInfoByUserName(vo.getUsername());
        if (userInfo == null) {
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
        //3.判断密码
        if (!passwordEncoder.matches(vo.getPassword(), userInfo.getPassword())) {
            return R.error(ResponseCode.SYSTEM_PASSWORD_ERROR.getMessage());
        }

        //4.属性复制 两个类之间属性一致
        LoginRespVo respVo = new LoginRespVo();
        BeanUtils.copyProperties(userInfo, respVo);

        return R.ok(respVo);
    }

    /*
     * 生成登录效验码
     * */
    @Override
    public R<Map> genCaptchaCode() {
        //1.生成随机效验码,长度为4
        String checkCode = RandomStringUtils.randomNumeric(4);
        //2.生成一个类似sessionId的id作为key然后效验码作为value保存在redis下,设置60s有效期
        String sessionId = String.valueOf(idWorker.nextId());
        redisTemplate.opsForValue().set(sessionId, checkCode, 60, TimeUnit.SECONDS);
        //3.组装响应的map对象
        HashMap<String, String> map = new HashMap<>();
        map.put("code", checkCode);
        map.put("rkey", sessionId);

        //4.返回组装数据
        return R.ok(map);
    }


}
